package com.ruoyi.labdata.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.labdata.service.ILabFlowInstanceService;
import com.ruoyi.labdata.service.ILabOrgnizationService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.labdata.mapper.LabDeclarationFilingMapper;
import com.ruoyi.labdata.domain.LabDeclarationFiling;
import com.ruoyi.labdata.service.ILabDeclarationFilingService;

import javax.annotation.Resource;

/**
 * 组织机构申报备案Service业务层处理
 * 
 * @author lzlong
 * @date 2022-05-04
 */
@Service
public class LabDeclarationFilingServiceImpl implements ILabDeclarationFilingService 
{
    @Resource
    private LabDeclarationFilingMapper labDeclarationFilingMapper;

    @Autowired
    private ILabFlowInstanceService labFlowInstanceService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ILabOrgnizationService labOrgnizationService;

    /**
     * 查询组织机构申报备案
     * 
     * @param instanceId 组织机构申报备案ID
     * @return 组织机构申报备案
     */
    @Override
    public LabDeclarationFiling selectLabDeclarationFilingById(Long instanceId)
    {
        return labDeclarationFilingMapper.selectLabDeclarationFilingById(instanceId);
    }

    /**
     * 查询组织机构申报备案列表
     * 
     * @param labDeclarationFiling 组织机构申报备案
     * @return 组织机构申报备案
     */
    @Override
    public List<LabDeclarationFiling> selectLabDeclarationFilingList(LabDeclarationFiling labDeclarationFiling)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        labDeclarationFiling.setCreateBy(user.getUserName());
        return labDeclarationFilingMapper.selectLabDeclarationFilingList(labDeclarationFiling);
    }

    /**
     * 新增组织机构申报备案
     * 
     * @param labDeclarationFiling 组织机构申报备案
     * @return 结果
     */
    @Override
    public int insertLabDeclarationFiling(LabDeclarationFiling labDeclarationFiling)
    {
        SysUser user = sysUserService.selectUserByUserName(labDeclarationFiling.getCreateBy());
        labDeclarationFiling.setCreateTime(DateUtils.getNowDate());
        labDeclarationFiling.setCreateUserId(user.getUserId());
        labDeclarationFiling.setCreateOrgId(user.getDeptId());
        labDeclarationFiling.setCreateTime(new Date());
        if (null == labDeclarationFiling.getInstanceId()){
            // 保存流程信息
             labFlowInstanceService.insertLabFlowInstance(labDeclarationFiling);
            labOrgnizationService.updateLabOrgnizationInstanceId(labDeclarationFiling);
        }
        return labDeclarationFilingMapper.insertLabDeclarationFiling(labDeclarationFiling);
    }

    /**
     * 修改组织机构申报备案
     * 
     * @param labDeclarationFiling 组织机构申报备案
     * @return 结果
     */
    @Override
    public int updateLabDeclarationFiling(LabDeclarationFiling labDeclarationFiling)
    {

        SysUser user = sysUserService.selectUserByUserName(labDeclarationFiling.getCreateBy());
        labDeclarationFiling.setCreateOrgId(user.getDeptId());
        labFlowInstanceService.updateLabFlowInstance(labDeclarationFiling);
        return labDeclarationFilingMapper.updateLabDeclarationFiling(labDeclarationFiling);
    }

    @Override
    public int updateLabDeclarationFilingStatus(LabDeclarationFiling labDeclarationFiling) {
        return labDeclarationFilingMapper.updateLabDeclarationFiling(labDeclarationFiling);
    }

    /**
     * 批量删除组织机构申报备案
     * 
     * @param instanceIds 需要删除的组织机构申报备案ID
     * @return 结果
     */
    @Override
    public int deleteLabDeclarationFilingByIds(Long[] instanceIds)
    {
        return labDeclarationFilingMapper.deleteLabDeclarationFilingByIds(instanceIds);
    }

    /**
     * 删除组织机构申报备案信息
     * 
     * @param instanceId 组织机构申报备案ID
     * @return 结果
     */
    @Override
    public int deleteLabDeclarationFilingById(Long instanceId)
    {
        return labDeclarationFilingMapper.deleteLabDeclarationFilingById(instanceId);
    }

    // 申报备案流程提交
    @Override
    public int commitLabDeclarationFiling(LabDeclarationFiling labDeclarationFiling) {
        SysUser user = sysUserService.selectUserByUserName(labDeclarationFiling.getCreateBy());
        labDeclarationFiling.setCreateTime(DateUtils.getNowDate());
        labDeclarationFiling.setCreateUserId(user.getUserId());
        labDeclarationFiling.setCreateOrgId(user.getDeptId());
        labDeclarationFiling.setCreateTime(new Date());
        if (null == labDeclarationFiling.getInstanceId()){
            // 保存流程信息
            labFlowInstanceService.insertLabFlowInstance(labDeclarationFiling);
            labOrgnizationService.updateLabOrgnizationInstanceId(labDeclarationFiling);
            // 保存申报备案流程相关信息
            labDeclarationFilingMapper.insertLabDeclarationFiling(labDeclarationFiling);
        }
        return labFlowInstanceService.commitFlow(labDeclarationFiling);
    }
}
